package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.AtributosDAO;
import com.cs.beneficios.common.ConnectionFactory;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.io.IOException;
import com.cs.paoyer.administration.business.model.UserVO;

import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class IngresarRestricAction extends Action
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    
    HttpSession session = request.getSession();
    //Proceso para la auditoria
    UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
    String terminalAddress = request.getRemoteAddr();
    String strTerminal = ("WEB="+terminalAddress);
    Calendar c = Calendar.getInstance(); 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");   
    //Fin del proceso
 
    String strCodSub = (String)session.getAttribute("codsubpro");
    session.setAttribute("codsubpro",strCodSub);
    String strCodAtri = (String)session.getAttribute("codatributo");
    int continuar = 0;
    Object oboperando1 = request.getParameter("operando1");
    String strOperando1 = "";
    String strOperando = "";
    String strCampo1 = "";
    if(oboperando1 == null)
    {
      strOperando = request.getParameter("descVflex");
      if(!strOperando.equals("nulo"))
      {
        String ArrOperando[] = strOperando.split("~");
        String stroper =  ArrOperando[0];
        if(!stroper.equals("null"))
        {
          strOperando1 = ArrOperando[0];
          strCampo1 = ArrOperando[1];
        }else
        {
          strOperando1 = ArrOperando[1];
          if(strOperando1.equals("nulo"))
          {
            continuar = 1;
          }
        }
      }else
      {
        request.setAttribute("msg2","No se selecciono ninguna opcion de la lista de valores");
        request.setAttribute("msg1","La restriccion no se ingreso correctamente");
      }
        
    }else
    {
      strOperando1 = request.getParameter("operando1");
    }
            
    if (continuar == 0)
    {
      String strOperador = request.getParameter("operador");
      String strOperando2 = request.getParameter("operando2");
      String strDescripcion = request.getParameter("descripcion");
      String strAlerta = request.getParameter("alerta");
      String strVigencia = request.getParameter("vigencia");
      String strArr[] = {strDescripcion,strOperador,strOperando1,strOperando2,
                         strAlerta,strVigencia,strCampo1,
                         user.getName(), dateFormat.format(c.getTime()),strTerminal,
                         user.getName(),dateFormat.format(c.getTime()),strTerminal};

      //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	  ConnectionFactory cf = new ConnectionFactory();
      AtributosDAO atriDAO = new AtributosDAO(cf);
     
      try
      {
        int iInsertarBien = atriDAO.InsertRestriccion(strArr);
        if(iInsertarBien!=0)
        {
          int iCodMaxRestri = atriDAO.getCodMaxRestriccion();
          String strArrAct[] = {user.getName(), dateFormat.format(c.getTime()),strTerminal};
          int iActualizarAtri = atriDAO.ActuCampoRestAtri(strCodSub,strCodAtri,iCodMaxRestri,strArrAct);
        }else
        {
          request.setAttribute("msg1","La restriccion no se ingreso correctamente");
        }
        cf.closeConnection(atriDAO.getConn());
      
      }catch(SQLException e){
		System.err.println("/******** TDSUB - cwsub");
		e.printStackTrace();
		System.err.println("TDSUB - cwsub ********/");
      }
    }else
    {
      request.setAttribute("msg1","La restriccion no se ingreso correctamente");
    }
    
  
    
    return mapping.findForward("success");
  }
}